package com.free.semantic.common;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class EqualFormula {

    public EqualFormula(String leftConceptName, String leftObjectName, String rightConceptName, String rightObjectName) {
        this.leftConceptName = leftConceptName;
        this.leftObjectName = leftObjectName;
        this.rightConceptName = rightConceptName;
        this.rightObjectName = rightObjectName;
    }


    private String leftConceptName;

    private String leftObjectName;

    private String rightConceptName;

    private String rightObjectName;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        EqualFormula that = (EqualFormula) o;
        return leftConceptName.equals(that.leftConceptName) && leftObjectName.equals(that.leftObjectName)
                && rightConceptName.equals(that.rightConceptName) && rightObjectName.equals(that.rightObjectName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(leftConceptName, leftObjectName, rightConceptName, rightObjectName);
    }


    @Override
    public String toString() {
        return String.format("[%s]:%s=[%s]:%s", leftConceptName, leftObjectName, rightConceptName, rightObjectName);
    }

    public static EqualFormula parseSimple(String formula) {
        String[] parts = formula.split("=");
        assert parts.length == 2;
        return new EqualFormula("", parts[0].trim(), "", parts[1].trim());
    }

    public static EqualFormula parse(String leftConceptName, String rightConceptName, String rawFormula) {
        if (StringUtils.isBlank(rawFormula)) {
            throw new RuntimeException("公式为空");
        }
        String[] factor = rawFormula.split("=");
        if (factor.length != 2) {
            throw new RuntimeException(String.format("公式 %s 不包含等号", rawFormula));
        }
        return new EqualFormula(leftConceptName, factor[0], rightConceptName, factor[1]);
    }


    public String getLeftConceptName() {
        return leftConceptName;
    }

    public String getLeftObjectName() {
        return leftObjectName;
    }

    public String getRightConceptName() {
        return rightConceptName;
    }

    public String getRightObjectName() {
        return rightObjectName;
    }

}
